package com.lanou.bbs.controller;

import com.lanou.bbs.entity.*;
import com.lanou.bbs.service.FloorService;
import com.lanou.bbs.service.ReplyService;
import com.lanou.bbs.service.TopicService;
import com.lanou.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author 张贵宏
 * @Date 18/6/19 下午7:02
 */
@Controller
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    private TopicService topicService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private UserService userService;
    @Autowired
    private FloorService floorService;

    @ResponseBody
    @RequestMapping("/findAll")
    public List<TopicAndUser> findAll() {
        List<TopicAndUser> topicAndUserList = new ArrayList<>();
        List<BbsTopic> bbsTopicList = topicService.findAll();

        for (BbsTopic bbsTopic : bbsTopicList) {
            User user = userService.findById(bbsTopic.getUid());
            TopicAndUser topicAndUser = new TopicAndUser();
            topicAndUser.setUser(user).setBbsTopic(bbsTopic);

            topicAndUserList.add(topicAndUser);
        }
        return topicAndUserList;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public Map<String, Object> findById(int tid,HttpSession session) {

        Map<String, Object> map = new HashMap<>();

        BbsTopic bbstopic = topicService.findById(tid);
        User userTopic = userService.findById(bbstopic.getUid());
        TopicAndUser topicAndUser = new TopicAndUser();

        topicAndUser.setBbsTopic(bbstopic).setUser(userTopic);

        List<BbsReply> replies = replyService.findByTid(tid);
        List<ReplyAndUser> replyAndUserList = new ArrayList<>();
        if (replies.size() > 0) {
            for (BbsReply reply : replies) {
                ReplyAndUser replyAndUser = new ReplyAndUser();
                User user = userService.findById(reply.getUid());
                replyAndUser.setBbsReply(reply).setUser(user);


                List<FloorAndUser> floorAndUserList = new ArrayList<>();
                List<BbsFloor> bbsFloorList = floorService.findByRid(reply.getRid());
                for (BbsFloor bbsFloor : bbsFloorList) {
                    FloorAndUser floorAndUser = new FloorAndUser();
                    User userFloor = userService.findById(bbsFloor.getUid());
                    floorAndUserList.add(floorAndUser.setBbsFloor(bbsFloor).setUser(userFloor));
                }

                replyAndUser.setFloorAndUserList(floorAndUserList);

                replyAndUserList.add(replyAndUser);
            }
        }

        User user = (User) session.getAttribute("user");
        String kname = user.getNickname();
        map.put("replyAndUserList", replyAndUserList);
        map.put("topicAndUser", topicAndUser);
        map.put("kname",kname);
        return map;
    }

    @ResponseBody
    @RequestMapping("/send")
    public void send(BbsTopic bbsTopic, HttpSession session) {

        User user = (User) session.getAttribute("user");
//        User user1 = new User();
//        user1.setUid(4);

        Date dt = new Date();
        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
        String format = dFormat.format(dt);
        bbsTopic.setUid(user.getUid()).setTime(format).setTstate(0).setReplyCount(0);
        topicService.send(bbsTopic);
    }
}
